import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router);

const router = new Router({
  // mode: "history",
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/',
      component: () => import('../components/common/Base.vue'),
      meta: {
        title: '公共部分'
      },
      children: [
        {
          path: '/index',
          component: () => import('../components/page/Home.vue'),
          meta: {
            title: '系统首页'
          }
        },
        {
          path: '/chart-simple',
          component: () => import('../components/page/EchartsSimple.vue'),
          meta: {
            title: '简单图表'
          }
        },
        {
          path: '/chart-complex',
          component: () => import('../components/page/EchartsComplex.vue'),
          meta: {
            title: '复杂图表'
          }
        },
        {
          path: '/message-notification',
          component: () => import('../components/page/message-notification.vue'),
          meta: {
            title: '消息通知'
          }
        },
        {
          path: '/user-management',
          component: () => import('../components/page/user-management.vue'),
          meta: {
            title: '用户管理'
          }
        },
        {
          path: '/product-management',
          component: () => import('../components/page/product-management.vue'),
          meta: {
            title: '商品管理'
          }
        },
        {
          path: '/order-management',
          component: () => import('../components/page/order-management.vue'),
          meta: {
            title: '订单管理'
          }
        }
      ]
    },
    {
      path: '/login',
      component: () => import('../components/page/Login.vue')
    },
    {
      path: '/error',
      component: () => import('../components/page/Error.vue')
    },
    {
      path: '/404',
      component: () => import('../components/page/404.vue')
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 在进入路由前进行一些逻辑处理，比如路由权限验证
  const isAuthenticated = localStorage.getItem('username') !== null;
  if (to.path !== '/login' && !isAuthenticated) {
    // 如果用户未登录且访问的不是登录页，则重定向到登录页
    next('/login');
  } else {
    next(); // 继续路由导航
  }
})

export default router;
